package com.dk.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dk.pojo.TPerson;
import com.dk.pojo.TRole;
import com.dk.service.PerosnService;
import com.dk.service.RoleService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("person")
public class PerosnController {
    @Reference
    private PerosnService perosnService;
    @Reference
    private RoleService roleService;
    @RequestMapping("toShow")
    public String toShow(){
        return "person/show";
    }
    @RequestMapping("getInfo")
    @ResponseBody
    public List<TPerson> getInfo(String pname){
        List<TPerson> list = perosnService.getInfo(pname);
        return list;
    }
    @RequestMapping("toAdd")
    public String toAdd(Model model){
        List<TRole> list = roleService.getInfo();
        model.addAttribute("list",list);
        return "person/add";
    }
    @RequestMapping("increase")
    @ResponseBody
    public boolean increase(TPerson tPerson, @RequestParam("rid")Integer[] rid){
       return perosnService.insert(tPerson ,rid) > 0;
    }
    @RequestMapping("toUpdate/{id}")
    public String  toUpdate(@PathVariable("id") Integer id, Model model){
        TPerson person = perosnService.selectById(id);
        List<TRole> list = roleService.getInfo();
        List<Integer> rids = roleService.selectPersonRoleByPid(id);
        model.addAttribute("person",person);
        model.addAttribute("list",list);
        model.addAttribute("rids",rids);
        return "person/update";
    }
    @RequestMapping("updateInfo")
    @ResponseBody
    public boolean upateInfo(TPerson tPerson,@RequestParam("roleId")Integer[] roleId){
       return perosnService.updateInfo(tPerson,roleId) > 0;
    }
    @RequestMapping("deleteById")
    @ResponseBody
    public boolean deleteById(Integer pid){
     return perosnService.deleteById(pid) > 0;
    }
    @RequestMapping("toLogin")
    public String toLogin(){
        return "login";
    }
    @RequestMapping("login")
    public String loginInfo(TPerson person,Model model){
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(person.getPname(),person.getPwd());
        subject.login(token);
        try{
            subject.login(token);
        }catch (UnknownAccountException e1){
            model.addAttribute("msg","账号不存在");
            return "login";
        }catch (IncorrectCredentialsException e2){
            model.addAttribute("msg","密码错误");
            return "login";
        }
        return "redirect:/index";
    }
    /*@RequestMapping("loginOut")
    public String loginOut(HttpServletRequest request){
        request.getSession().removeAttribute("loginUser");
        return "redirect:/";
    }*/
    @RequestMapping("checkPhone")
    @ResponseBody
    public Boolean checkPhone(String uname){
        return perosnService.selectcheckPhone(uname);

    }
    @RequestMapping("unauthorized")
    public String unauthorized(Model model){
        model.addAttribute("mesg","权限不足");
        return "unauthorized";
    }
}
